--荒野法术
local huangyefashu = fk.CreateSkill {
    name = "mcdh_huangyefashu"
}

huangyefashu:addEffect('viewas',{
    pattern = "slash",
    card_filter = function(self,player, to_select, selected)
        return #selected < 2
    end,
    view_as = function(self, cards)
        if #cards ~= 2 then return end
        local c = Fk:cloneCard("slash")
        c.skillName = self.name
        c:addSubcards(cards)
        return c
    end,
    before_use = function (self, player, use)
        use.extra_data = {}
        use.extra_data.huangyefashu = player
    end
})

huangyefashu:addEffect("prohibit",{
     prohibit_use = function(self, player, card)
        return player:hasSkill(huangyefashu.name) and card:isCommonTrick() and player:getMark("mcdh_huangyefashu") == 0
    end
})

huangyefashu:addEffect("targetmod",{
     bypass_distances = function (self, player, skill, card)
      return player:hasSkill(huangyefashu.name) and skill.trueName == "slash_skill" and card.skillName == "mcdh_huangyefashu"
    end
})

huangyefashu:addEffect(fk.Damaged,{
    is_delay_effect = true,
    can_trigger = function(self, event, target, player, data)
        local room = player.room
        local logic = room.logic
        if  data.card and table.contains(data.card.skillNames, "mcdh_huangyefashu") then
            local subcards = data.card:isVirtual() and data.card.subcards or { data.card.id }
            local damageEvent = logic:getCurrentEvent()
            local useEvent =  damageEvent:findParent(GameEvent.UseCard)
            return  useEvent and useEvent.data.extra_data and useEvent.data.extra_data.huangyefashu == player and #subcards > 0 and
                table.every(subcards, function(id) return player.room:getCardArea(id) == Card.Processing end)
        end
        return false
    end,
    on_cost = Util.TrueFunc,
    on_use = function(self, event, target, player, data)
        local room = player.room
        local cards = data.card:isVirtual() and data.card.subcards or { data.card.id }
        
        cards = table.filter(cards, function(id) return Fk:getCardById(id).type == Card.TypeTrick end)
        while not player.dead and #cards > 0 do
            room:setPlayerMark(player,"mcdh_huangyefashu",1)
            local use = room:askToUseRealCard(player, {
            pattern = cards,
            skill_name = huangyefashu.name,
            prompt = "#mcdh_huangyefashu-invoke",
            extra_data = {
            bypass_times = true,
            extra_use = true,
            expand_pile = cards,
            },
            skip = true,
        })
        room:setPlayerMark(player,"mcdh_huangyefashu",0)
        if use then
            table.removeOne(cards, use.card.id)
            room:useCard(use)
        else
            break
        end
    end
  end,
})

Fk:loadTranslationTable {
    ["mcdh_huangyefashu"] = "荒野法术",
    [":mcdh_huangyefashu"] = "你不能以此法外的方式使用普通锦囊牌：你可以将两张牌当无距离限制的【杀】使用或打出；" ..
        "当此【杀】造成伤害后，你依次可以使用其中的锦囊牌。",
    ["#mcdh_huangyefashu-invoke"] = "荒野法术:使用其中任意张锦囊牌",
}


return huangyefashu